<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 20150702
 * 按照区分新老用户计算的用户来源类型，统计首次登录用户在第二天，前7天，前14天，前21天的留存数据
 */
class RetainSourceController extends AdminBaseController {

    /**
     * 来源类型留存报表
     */
    public function index() {
        $this->setTitle(PubFunc::t('用户来源留存报表'));

        $filter = new PubFilter();
        $grid = new PubGrid();

        $userTypeOptions = array(
            'NewAndSec' => PubFunc::t('新用户与二次用户'),
            'NaturalRemain' => PubFunc::t('自然流量用户'),
            'NewAdRemain' => PubFunc::t('广告新用户'),
            'OldAdRemain' => PubFunc::t('广告老用户'),
            'SecAdRemain' => PubFunc::t('广告二次用户'),
        );

        $stateOptions = array('ALL' => PubFunc::t('多日留存'));

        $filter->addPlatformFilter()
                ->addFilter('time', 'timerange', PubFunc::t('日期'), array('value' => PubDateRange::getLastDayRange(7, strtotime('yesterday'))))
                ->addFilter('kpi', 'select', PubFunc::t('指标'), array('options' => $userTypeOptions, 'value' => key($userTypeOptions)))
                ->addFilter('state', 'hidden', '', array('value' => 'ALL'));

        $params = $filter->get();
        $params['server'] = 'ALL';

        $userType = $userTypeOptions[$params['kpi']];
        $header = RetainSourceService::indexHeader($userType);
        $data = RetainSourceService::indexData($params);

        foreach ($stateOptions as $k => $v) {
            if ($k == $params['state']) {
                $grid->addGrid($header, $data, array(
                    'name' => $v,
                    'order' => array(
                        array('RecDate', 'desc'),
                    ),
                ));
            } else {
                $grid->addLink($k, $v);
            }
        }

        $this->out['filter'] = $filter->show();
        $this->out['grid'] = $grid->show();
    }

}
